package com.bright.ghj.overt.web.controller;

import com.bright.ghj.common.pojo.vo.PageVO;
import com.bright.ghj.common.pojo.bo.RestResult;
import com.bright.ghj.overt.util.SecurityUtil;
import com.bright.ghj.overt.pojo.query.AsyncTaskMassageQuery;
import com.bright.ghj.overt.pojo.vo.AsyncMessageVO;
import com.bright.ghj.overt.service.AsyncMessageService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.RequiredArgsConstructor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.constraints.Min;
import javax.validation.constraints.NotBlank;
import javax.validation.constraints.NotNull;

/**
 * @author Tz
 * @date 2024/01/22 15:03
 * @Description 异步任务消息 这里只负责对异步任务通用的处理 具体的任务提交由具体的业务实现
 */
@RequestMapping("/async")
@RestController
@Validated
@RequiredArgsConstructor
@Api(tags = "异步任务消息")
public class AsyncMessageController {

    private final AsyncMessageService asyncMessageService;

    @ApiOperation(value = "获取异步执行任务的进度")
    @GetMapping("/getCompletionDegree")
    public RestResult getCompletionDegree(@NotBlank(message = "keyword不能为空") String keyword) {
        return RestResult.success(asyncMessageService.getCompletionDegree(keyword));
    }

    @ApiOperation(value = "撤销发送的异步任务")
    @PostMapping("/task/revoke")
    public RestResult revokeAsyncTask(@NotNull(message = "id不能为空") @Min(value = 1, message = "id不能小于1") @RequestParam("id") Integer id) {
        Integer revokeAsyncTask = asyncMessageService.revokeAsyncTask(id);
        return RestResult.success();
    }

    @ApiOperation(value = "查询异步任务列表")
    @PostMapping("/message/page")
    public RestResult pageAsyncMessage(@RequestBody AsyncTaskMassageQuery asyncTaskMassageQuery) {
        asyncTaskMassageQuery.setUsername(SecurityUtil.getLoginUser().getUsername());
        PageVO<AsyncMessageVO> pageAsyncMessage = asyncMessageService.pageAsyncMessage(asyncTaskMassageQuery);
        return RestResult.success(pageAsyncMessage);
    }

    @ApiOperation(value = "一键阅读指定状态类别消息")
    @PostMapping("/task/oneClickRead")
    public RestResult oneClickRead(@ApiParam(name = "statusCategory", value = "状态类别。 0、全部, 1、执行中, 2、执行成功, 3、执行失败", example = "0")
                                       @RequestParam("statusCategory") Integer statusCategory) {
        Integer clickReadSize = asyncMessageService.oneClickRead(statusCategory, SecurityUtil.getLoginUser().getUsername());
        if (clickReadSize <= 0) {
            return RestResult.fail("没有可读消息！");
        }
        return RestResult.success();
    }

    @ApiOperation(value = "阅读一条未读消息")
    @PostMapping("/task/read")
    public RestResult read(@NotNull(message = "id不能为空") @Min(value = 1, message = "id不能小于1") @RequestParam("id") Integer id) {
        Integer readSize = asyncMessageService.read(id);
        if (readSize <= 0) {
            return RestResult.fail("没有可读消息！");
        }
        return RestResult.success();
    }

    @ApiOperation(value = "获取异步执行任务的执行情况")
    @GetMapping("/getAsyncMessageExecInfo")
    public RestResult getAsyncMessageExecInfo() {
        String username = SecurityUtil.getLoginUser().getUsername();
        return RestResult.success(asyncMessageService.getAsyncTaskExecInfo(username));
    }

    @ApiOperation(value = "确认读取异步任务的执行情况")
    @GetMapping("/confirmReadingAsyncMessage")
    public RestResult confirmReadingAsyncMessage() {
        String username = SecurityUtil.getLoginUser().getUsername();
        asyncMessageService.confirmReadingExecResult(username);
        return RestResult.success();
    }

}
